package com.css.zfzx.sjcj.modules.hddccjproject.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.zfzx.sjcj.common.utils.UpdateUtil;
import com.css.zfzx.sjcj.modules.hddcB7_VolcanicDataTable.service.HddcB7VolcanicdatatableService;
import com.css.zfzx.sjcj.modules.hddccjproject.repository.HddcCjProjectNativeRepository;
import com.css.zfzx.sjcj.modules.hddccjproject.repository.HddcCjProjectRepository;
import com.css.zfzx.sjcj.modules.hddccjproject.repository.entity.HddcCjProjectEntity;
import com.css.zfzx.sjcj.modules.hddccjproject.service.HddcCjProjectService;
import com.css.zfzx.sjcj.modules.hddccjproject.service.HddcCjprotaskService;
import com.css.zfzx.sjcj.modules.hddccjproject.viewobjects.HddcCjProjectQueryParams;
import com.css.zfzx.sjcj.modules.hddccjproject.viewobjects.HddcCjProjectVO;
import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.org.user.repository.entity.UserEntity;
import com.css.bpm.platform.sys.dict.repository.entity.DictItemEntity;
import com.css.bpm.platform.utils.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author zhangping
 * @date 2020-11-26
 */
@Service
public class HddcCjProjectServiceImpl implements HddcCjProjectService {

	@Autowired
    private HddcCjProjectRepository hddcCjProjectRepository;
    @Autowired
    private HddcCjProjectNativeRepository hddcCjProjectNativeRepository;
    @Autowired
    private HddcCjprotaskService hddcCjprotaskService;

    @Override
    public JSONObject queryHddcCjProjects(HddcCjProjectQueryParams queryParams, int curPage, int pageSize,String sort,String order) {
        Page<HddcCjProjectEntity> hddcCjProjectPage = this.hddcCjProjectNativeRepository.queryHddcCjProjects(queryParams, curPage, pageSize, sort, order);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(hddcCjProjectPage);
        return jsonObject;
    }


    @Override
    public HddcCjProjectVO getHddcCjProject(String id) {
        HddcCjProjectEntity hddcCjProject = this.hddcCjProjectRepository.findById(id).orElse(null);
        HddcCjProjectVO hddcCjProjectVO = new HddcCjProjectVO();
        if(hddcCjProject!=null){
            BeanUtils.copyProperties(hddcCjProject, hddcCjProjectVO);
            UserEntity userOfPersonIds = PlatformAPI.getOrgAPI().getUserAPI().getUser(hddcCjProject.getPersonIds());
            String valuePersonIds = "";
            if(PlatformObjectUtils.isNotEmpty(userOfPersonIds)) {
                valuePersonIds = userOfPersonIds.getUserName();
            }
            hddcCjProjectVO.setPersonIdsName(valuePersonIds);
        }
        return hddcCjProjectVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcCjProjectEntity saveHddcCjProject(HddcCjProjectEntity hddcCjProject) {
        String uuid = UUIDGenerator.getUUID();
        hddcCjProject.setId(uuid);
        hddcCjProject.setCreateUser(PlatformSessionUtils.getUserId());
        hddcCjProject.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        hddcCjProject.setIsValid("1");
        this.hddcCjProjectRepository.save(hddcCjProject);
        return hddcCjProject;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcCjProjectEntity updateHddcCjProject(HddcCjProjectEntity hddcCjProject) {
        HddcCjProjectEntity entity = hddcCjProjectRepository.findById(hddcCjProject.getId()).get();
        UpdateUtil.copyNullProperties(entity,hddcCjProject);
        hddcCjProject.setUpdateUser(PlatformSessionUtils.getUserId());
        hddcCjProject.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        this.hddcCjProjectRepository.save(hddcCjProject);
        return hddcCjProject;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String deleteHddcCjProjects(List<String> ids) {
        List<HddcCjProjectEntity> hddcCjProjectList = this.hddcCjProjectRepository.findAllById(ids);
        if(!PlatformObjectUtils.isEmpty(hddcCjProjectList) && hddcCjProjectList.size() > 0) {
            for(HddcCjProjectEntity hddcCjProject : hddcCjProjectList) {
//                this.hddcCjProjectRepository.delete(hddcCjProject);
                //判断当前项目下是否关联任务，若未关联则提示："该项目关联了任务，不能删除！"
                Integer taskCount = hddcCjProjectNativeRepository.queryHddcCjTasksByProjectId(hddcCjProject.getProjectId());
                /*if(taskCount >0){
                    return "所删项目:"+hddcCjProject.getProjectName()+"关联了任务，不能删除！";
                }*/
                //删除任务
                hddcCjProjectRepository.deletetaskname(hddcCjProject.getProjectName());
                ArrayList<String> list = new ArrayList<>();
                list.add(hddcCjProject.getProjectName());
                hddcCjprotaskService.aa(list);
                //hddcB7VolcanicdatatableService.deleteByProjectId(list);
                hddcCjProject.setUpdateUser(PlatformSessionUtils.getUserId());
                hddcCjProject.setIsValid("0");
                hddcCjProject.setUpdateTime(new Date());
                this.hddcCjProjectRepository.save(hddcCjProject);
            }
        }
        return "删除成功";
    }



    @Override
    public List<DictItemEntity> getValidDictItemsByDictCode(String dictCode) {
        List<DictItemEntity> validDictItems = PlatformAPI.getDictAPI().getValidDictItemsByDictCode(dictCode);
        return validDictItems;
    }

    @Override
    public List<HddcCjProjectEntity> findAll() {
//        List<HddcCjProjectEntity> hddcCjProjectList = this.hddcCjProjectRepository.findAll();
        return hddcCjProjectNativeRepository.queryHddcCjProjectName();
    }

    @Override
    public List<HddcCjProjectEntity> findHddcCjProjectEntityByPersonId(String personid) {
        List<HddcCjProjectEntity> hddcCjProjectList = this.hddcCjProjectRepository.findHddcCjProjectEntityByPersonId(personid);
        return hddcCjProjectList;
    }

    @Override
    public JSONObject queryHddcCjProjectsByPersonId(String personId, int curPage, int pageSize) {
        Page<HddcCjProjectEntity> hddcCjProjectPage = this.hddcCjProjectNativeRepository.queryHddcCjProjectsByPersonId(personId, curPage, pageSize);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(hddcCjProjectPage);
        return jsonObject;
    }

}
